function StringBuffer(str){
	this._strings = [];
	if(str != undefined && str != null && str != ""){
		this._strings.push(str);
	}
}

StringBuffer.prototype.append = function(str){
	this._strings.push(str);
	return this;
}

StringBuffer.prototype.toString = function(){
	return this._strings.join("");
}

/**
�ж��Ƿ�Ϊ��
*/
function isEmpty(str){
	if(str == undefined || str == null || str == "" || str == "null"){
		return true;
	}else{
		return false;
	}
}


function Node(id, pid, caption, title, url, target){
	this.id = id;
	this.pid = pid;
	this.caption = caption;
	this.title = title;
	this.url = url;
	this.target = target;
}

function myMenu(rootPid) {
	this.rootPid = rootPid;				//���õ�һ��ڵ��pid
	this.childNodes = [];				//�ӽڵ�����
}


//���ýڵ�
myMenu.prototype.addNodes = function(id, pid, caption, title, url, target){
	this.childNodes.push(new Node(id, pid, caption, title, url, target));
}

/**
��ɵ�һ��˵�
*/
myMenu.prototype.createMenu = function(){
	var sb = new StringBuffer("");
	if(this.childNodes != null && this.childNodes.length > 0){
		sb.append("<ul id='nav'>");
		var pid = this.rootPid;
		var rootNodes = this.findChildren(pid);
		for(var i=0;i<rootNodes.length;i++){
			var child_child = this.findChildren(rootNodes[i].id);
			sb.append("<li class='top'>");
			sb.append("<a title='").append(rootNodes[i].title).append("' class='top_link' target='").append(rootNodes[i].target).append("'");
			if(isEmpty(rootNodes[i].url)){
				sb.append(" href='#'");
			}else{
				sb.append(" href='").append(rootNodes[i].url).append("'");
			}
			sb.append("><span");
			if(child_child.length > 0){
				sb.append(" class='down'");
			}
			sb.append(">").append(rootNodes[i].caption).append("</span></a>");
			if(child_child.length > 0){
				this.createChildMenu(sb, rootNodes[i].id, child_child, true);
			}
			sb.append("</li>");
		}
		sb.append("</ul>");
	}
	document.write(sb.toString());
	stuHover();
}

function stuHover() {
	var cssRule;
	var newSelector;
	for (var i = 0; i < document.styleSheets.length; i++)
		for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
			{
			cssRule = document.styleSheets[i].rules[x];
			if (cssRule.selectorText.indexOf("LI:hover") != -1)
			{
				 newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
				document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
			}
		}
	var getElm = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<getElm.length; i++) {
		getElm[i].onmouseover=function() {
			this.className+=" iehover";
		}
		getElm[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}


/**
����Ӳ˵�
*/
myMenu.prototype.createChildMenu = function(sb, pid, child, bool){
	sb.append("<ul");
	if(bool){
		sb.append(" class='sub'");
	}
	sb.append(">");
	for(var i=0;i<child.length;i++){
		var child_child = this.findChildren(child[i].id);
		sb.append("<li>");
		sb.append("<a title='").append(child[i].title).append("' target='").append(child[i].target).append("'");
		if(isEmpty(child[i].url)){
			sb.append(" href='#'");
		}else{
			sb.append(" href='").append(child[i].url).append("'");
		}
		if(child_child.length > 0){
			sb.append(" class='fly'");
		}
		
		sb.append(">").append(child[i].caption).append("</a>");
		if(child_child.length > 0){
			this.createChildMenu(sb, child[i].id, child_child, false);
		}
		sb.append("</li>");
	}
	sb.append("</ul>");
}

/**
�����ڵ���ӽڵ�
�������ڵ�������ӽ��Ķ�������
*/
myMenu.prototype.findChildren = function(id){
	var arr = [];
	if(this.childNodes.length > 0){
		for(var i=0;i<this.childNodes.length;i++){
			if(this.childNodes[i].pid == id){
				arr.push(this.childNodes.splice(i,1)[0]);
				i--;
			}
		}
	}
	return arr;
}